package com.winit.openapi.erp.controller;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.erp.spi.product.ReturnProductStorageService;
import com.winit.erp.spi.product.command.GetReturnProductStroageCommand;
import com.winit.erp.spi.product.command.QueryProductCommand;
import com.winit.erp.spi.product.command.QueryProductDetailsCommand;
import com.winit.erp.spi.product.command.QueryReturnProductStroageCommand;
import com.winit.erp.spi.product.vo.GetReturnProductStroageVo;
import com.winit.erp.spi.product.vo.Product;
import com.winit.erp.spi.product.vo.QueryReturnProductStroageVo;
import com.winit.erp.spi.product.vo.ReturnProductStorageVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;

/**
 * 退货商品库存Controller
 * @author qibin.tang
 *
 */
@Controller
@RequestMapping(value = "/erp/returnProductStorage")
public class ReturnProductStorageController extends BaseController {

    @Resource
    private ReturnProductStorageService returnProductStorageService;

    /**
     * 当客户选择退货商品出库时，需将退货商品转换为具体出库商品
     * 退货商品：退货商品维护为原sku的商品   ，具体数据为：商品编码（return）+规格
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryReturnProductStorage")
    public String queryReturnProductStorage(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	Object obj = requestMsg.getData();
    	QueryReturnProductStroageCommand command = new QueryReturnProductStroageCommand();
    	List<QueryReturnProductStroageVo> list = new ArrayList<QueryReturnProductStroageVo>();
		if (obj instanceof JSONObject) {
			JSONObject json = (JSONObject) obj;
			JSONArray array = json.getJSONArray("list");
			for (Object item : array) {
				JSONObject jsonObject = (JSONObject) item;
				QueryReturnProductStroageVo queryReturnProductStroageVo = new QueryReturnProductStroageVo();
				queryReturnProductStroageVo.setFormerProductId(jsonObject.getLong("formerProductId"));
				queryReturnProductStroageVo.setQtyDelivery(jsonObject.getLong("qtyDelivery"));
				queryReturnProductStroageVo.setWarehouseId(jsonObject.getLong("warehouseId"));
				list.add(queryReturnProductStroageVo);
			}
			command.setList(list);
		} else {
			throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
		}
		
		List<ReturnProductStorageVo> returnlist = returnProductStorageService.queryReturnProductStorage(command);
		responseMsg.setData(returnlist);
        return SUCCESS;
    }
    
    /**
     * 根据仓库ID与原商品ID获取退货商品信息
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryReturnProduct")
    public String queryReturnProduct(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	Object obj = requestMsg.getData();
    	GetReturnProductStroageCommand command = new GetReturnProductStroageCommand();
    	List<GetReturnProductStroageVo> list = new ArrayList<GetReturnProductStroageVo>();
		if (obj instanceof JSONObject) {
			JSONObject json = (JSONObject) obj;
			JSONArray array = json.getJSONArray("list");
			for (Object item : array) {
				JSONObject jsonObject = (JSONObject) item;
				GetReturnProductStroageVo getReturnProductStroageVo = new GetReturnProductStroageVo();
				getReturnProductStroageVo.setFormerProductId(jsonObject.getLong("formerProductId"));
				getReturnProductStroageVo.setWarehouseId(jsonObject.getLong("warehouseId"));
				list.add(getReturnProductStroageVo);
			}
			command.setList(list);
		} else {
			throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
		}
		List<ReturnProductStorageVo> returnlist = returnProductStorageService.getReturnProductStorage(command);
		responseMsg.setData(returnlist);
        return SUCCESS;
    }
    
    

    @RequestMapping(value = "/queryProductByBPartnerId")
    public String queryProductByBPartnerId(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();

        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            QueryProductCommand command = new QueryProductCommand();
            command.setBpartnerId(json.getLong("bpartnerId"));
            command.setWarehouseCode(json.getString("warehouseCode"));
            List<Product> list = returnProductStorageService.queryProductByBPartnerId(command);
            responseMsg.setData(list);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }
    
    @RequestMapping(value = "/queryProductDetails")
    public String queryProductDetails(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            QueryProductDetailsCommand command = new QueryProductDetailsCommand();
            command.setWarehouseCode(json.getString("warehouseCode"));
            command.setSku(json.getString("sku"));
            Product product = returnProductStorageService.queryProductDetails(command);
            responseMsg.setData(product);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }

}
